package com.youthburin.base.taglib;

import java.io.IOException;

import javax.servlet.jsp.JspException;

import org.apache.commons.lang.StringUtils;

import com.youthburin.base.util.DwzPagination;

public class DwzPaginationTag extends BaseTag {
	private static final long serialVersionUID = 1L;
	private String conditions;
	private String orderField;
	private String orderDirection;
	private String url;

	@Override
	public int doEndTag() throws JspException {
		DwzPagination page = (DwzPagination) pageContext.getRequest().getAttribute(
				DwzPagination.DWZPAGINATION_PROPERTY_KEY);
		if (page != null) {
			StringBuffer str = new StringBuffer();

			str.append("<div class='panelBar'>");
			str.append("	<form id='pagerForm' action='")
					.append(pageContext.getRequest().getServletContext().getContextPath()).append(url)
					.append("' method='post'>");
			str.append("		<input type='hidden' name='pageNum' value='").append(page.getPageNum()).append("'/>");
			str.append("		<input type='hidden' name='numPerPage' value='").append(page.getNumPerPage()).append("'/>");
			str.append("		<input type='hidden' name='orderField' value='").append(orderField).append("'/>");
			str.append("		<input type='hidden' name='orderDirection' value='").append(orderDirection).append("'/>");
			str.append("		").append(getConditionFields());
			str.append("	</form>");
			str.append("	<div class='pages'>");
			str.append("		<span>").append(getMessage("page.pagination.display")).append("</span>");
			str.append("		<select class='combox' name='numPerPage' onchange='navTabPageBreak({numPerPage:this.value})'>");
			if (page.getNumPerPageList() != null && page.getNumPerPageList().length > 0) {
				for (String numPerPage : page.getNumPerPageList()) {
					if (Integer.valueOf(numPerPage) == page.getNumPerPage()) {
						str.append("<option value='").append(numPerPage).append("' selected>").append(numPerPage)
								.append("</option>");
					} else {
						str.append("<option value='").append(numPerPage).append("'>").append(numPerPage)
								.append("</option>");
					}

				}
			}
			str.append("		</select>");
			str.append("		<span>").append(getMessage("page.pagination.item")).append(getMessage("page.commons.comma"))
					.append(getMessage("page.pagination.all")).append(" ").append(page.getTotalCount()).append(" ")
					.append(getMessage("page.pagination.item")).append("</span>");
			str.append("	</div>");
			str.append("	<div class='pagination' targetType='navTab' totalCount='").append(page.getTotalCount())
					.append("' numPerPage='").append(page.getNumPerPage()).append("' pageNumShown='")
					.append(page.getPageNumShown()).append("' currentPage='").append(page.getPageNum())
					.append("'></div>");
			str.append("</div>");

			try {
				pageContext.getOut().write(str.toString());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return SKIP_BODY;
	}

	public String getConditionFields() {
		StringBuffer str = new StringBuffer();
		if (StringUtils.isNotBlank(conditions)) {
			for (String condition : conditions.split(",")) {
				String name = DwzPagination.SEARCHFILEDNAME_PREFIX + StringUtils.trim(condition);
				String opName = DwzPagination.SEARCHFIELDOPNAME_PREFIX + StringUtils.trim(condition);

				String value = StringUtils.trimToEmpty(pageContext.getRequest().getParameter(name));
				String opValue = StringUtils.trimToEmpty(pageContext.getRequest().getParameter(opName));

				str.append("<input type='hidden' name='").append(name).append("' value='").append(value).append("'>");
				str.append("<input type='hidden' name='").append(opName).append("' value='").append(opValue)
						.append("'>");
			}
		}

		return str.toString();
	}

	public String getConditions() {
		return conditions;
	}

	public void setConditions(String conditions) {
		this.conditions = conditions;
	}

	public String getOrderField() {
		return orderField;
	}

	public void setOrderField(String orderField) {
		this.orderField = orderField;
	}

	public String getOrderDirection() {
		return orderDirection;
	}

	public void setOrderDirection(String orderDirection) {
		this.orderDirection = orderDirection;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
}
